package cn.blog.controller.front;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.blog.controller.BaseServlet;
import cn.blog.entity.origin.User;
import cn.blog.service.front.CompileHeadService;
import cn.blog.service.front.LoginService;

@WebServlet("/front/login")
public class LoginServlet extends BaseServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getSession().setAttribute("uname", "登录");
		String login = "登录";
		String loginUrl="front/login";
		
		
		
		String reg = "注册";
		String regUrl = "front/register";
		
		req.setAttribute("login", login);
		req.setAttribute("reg", reg);
		req.setAttribute("loginUrl", loginUrl);
		req.setAttribute("regUrl", regUrl);
		forward("front/login");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String url = (String) req.getSession().getAttribute("url");
		String uname = req.getParameter("uname").toString();
		String password = req.getParameter("password").toString();
		String frontyzm = req.getParameter("yzm").toString(); // 用户输入的验证码
		String yzm = req.getSession().getAttribute("yzm").toString();
		LoginService loginService = new LoginService();
		List userNameAll = loginService.getUserNameAll();

		if (uname == null || uname == "") {
			req.getSession().setAttribute("unameLabel", "用户名不能为空！");
			redirect("/front/login");
			return;
		} else {
			req.getSession().setAttribute("unameLabel", "");
			if (password == null || password == "") {
				req.getSession().setAttribute("passwordLabel", "密码不能为空！");
				redirect("/front/login");
				return;
			} else {
				req.getSession().setAttribute("passwordLabel", "");
				if (frontyzm == null || frontyzm == "") {
					req.getSession().setAttribute("frontyzmLabel", "验证码不能为空！");
					redirect("/front/login");
					return;
				} else {
					if (frontyzm.equals(yzm)) {
						req.getSession().setAttribute("frontyzmLabel", "");
					} else {
						req.getSession().setAttribute("frontyzmLabel", "验证码不正确！");
						redirect("/front/login");
						return;
					}
				}
			}
		}

		for (int i = 0; i < userNameAll.size(); i++) {
			if (userNameAll.get(i).equals(uname)) {
				req.getSession().setAttribute("unameLabel", " ");
				if (password.equals(loginService.getUserPassword(uname))) {
					req.getSession().setAttribute("passwordLabel", "");
					req.getSession().setAttribute("uname", uname);
					
					CompileHeadService chs = new CompileHeadService();
					//根据用户名查询用户信息
					User userInfo = chs.getUser(uname);
					req.getSession().setAttribute("userId", userInfo.getUserId());
					req.getSession().setAttribute("url", "");
					if(url==null ||url=="")url="/front/blog";
					redirect(url);
					return;
				} else {
					req.getSession().setAttribute("passwordLabel", "密码不正确！");
					redirect("/front/login");
					return;
				}

			} else {
				req.getSession().setAttribute("unameLabel", " 用户名不存在");
				
				
			}
		}
		redirect("/front/login");

	}

}
